package cloudspace.vm.javassist.tests;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import _test.wrap.MyFile;
import cloudspace.vm.VM;
import junit.framework.TestCase;

public class MyFileSuperTest extends TestCase
{
	public static final String classToTest = "_test.wrap.MyFile";
	public String realPath = "/home/mjw87/workspace/cloudspace/src";
	// public ByteCodeRuntime bct;
	public static VM virtualM;

	public void setUp()
	{

		virtualM = VM.currentVM(realPath);
		virtualM.registerThread();

	}

	public File createClass()
	{
		try
		{
			Class<?> myFileClass = virtualM.getLoader().loadClass(classToTest);
			return (File) myFileClass.getConstructor(
					new Class<?>[] { java.lang.String.class }).newInstance(
					"widget");
		}
		catch (ClassNotFoundException e)
		{
			fail("Could not load");
		}
		catch (IllegalArgumentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SecurityException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InstantiationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (NoSuchMethodException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public void testGetAbsoluteFile()
	{
		File myFile = createClass();
		File absFile = myFile.getAbsoluteFile();
		assertEquals(realPath + "/foo.bar", absFile.getPath());

	}

	public void testGetAbsolutePath()
	{
		File myFile = createClass();
		File absFile = myFile.getAbsoluteFile();
		assertEquals(realPath + "/foo.bar", absFile.getPath());
	}

	public void testGetCanonicalFile()
	{
		File myFile = createClass();
		boolean equals = myFile.equals(new File("/foo/bar/blah"));
		assertTrue(equals);
	}

	public void testGetCanonicalPath()
	{
		File myFile = createClass();
		try
		{
			assertEquals(realPath + "/foo.bar", myFile.getCanonicalPath());
		}
		catch (IOException e)
		{
			fail();
			e.printStackTrace();
		}
	}

	public void testGetName()
	{
		File myFile = createClass();
		String getName = myFile.getName();
		assertEquals("foo.bar", getName);
	}

	public void testGetParent()
	{
		File myFile = createClass();
		String parent = myFile.getParent();
		assertEquals("foo.bar", parent);
	}

	public void testGetParentFile()
	{
		File myFile = createClass();
		File parent = myFile.getParentFile();
		assertEquals(realPath+"/foo.bar", parent.getPath());
	}

	public void testGetPath()
	{
		File myFile = createClass();
		String parent = myFile.getPath();
		assertEquals(realPath+"/foo.bar", parent);
	}

	public void testListFiles()
	{
		File myFile = createClass();
		File[] list = myFile.listFiles();
		for(File file : list)
		{
			if(!file.getPath().contains(realPath))
			{
				fail();
			}
		}
	}
	public void testToString()
	{
		File myFile = createClass();
		String toString = myFile.toString();
		assertEquals(realPath+"/foo.bar",toString);
	}

	public void testToURI()
	{
		File myFile = createClass();
		URI uri = myFile.toURI();
		assertNull(uri);
	}

	public void testToURL()
	{
		File myFile = createClass();
		URL url;
		try
		{
			url = myFile.toURL();
			assertNull(url);
		}
		catch (MalformedURLException e)
		{
			fail();
			e.printStackTrace();
		}
	}
}